package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterial;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterialList;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzMaterialMapper;
import com.jy.adscm.operation.mscyb.service.JqbzMaterialListService;
import com.jy.adscm.operation.mscyb.service.JqbzMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-25
 */
@Service
public class JqbzMaterialServiceImpl extends ServiceImpl<JqbzMaterialMapper, JqbzMaterial> implements JqbzMaterialService {
      @Autowired
      CgMaterialMapper cgMaterialMapper;
      @Autowired
    JqbzMaterialListService  jqbzMaterialListService;
    @Override
    public List<JqbzMaterial> selectByTid(String tid) throws AdscmSystemException {
        Wrapper<JqbzMaterial> wrapper =new EntityWrapper<>();
         wrapper.eq("tid",tid);

        List<JqbzMaterial> jqbzMaterials = super.selectList(wrapper);
        for (JqbzMaterial jqbzMaterial : jqbzMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzMaterial.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzMaterial.getName());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    jqbzMaterial.setNameName(cgMaterial.getName());
                }
            }
        }
        return jqbzMaterials;
    }

    @Override
    public Boolean approve(String tid, Token token) throws AdscmSystemException {
        //根据这个tid 查询到这个下面的所有的装配清单
        Wrapper<JqbzMaterial> jqbzMaterialWrapper=new EntityWrapper<>() ;
        jqbzMaterialWrapper.eq("tid",tid);
        List<JqbzMaterial> jqbzMaterials = super.selectList(jqbzMaterialWrapper);
        for (JqbzMaterial jqbzMaterial : jqbzMaterials) {
            if (jqbzMaterial.getIsAssembly()==1){
                continue;
            }
            //到原料库查询对应的库存
            List<JqylMaterial> jqylMaterials = jqbzMaterialListService.queryMaterial(jqbzMaterial.getName());
            double temp=0f;

            for (JqylMaterial jqylMaterial : jqylMaterials) {
                temp=temp+jqylMaterial.getRemainingnums();
            }
            if (jqbzMaterial.getNum()>temp){
                continue;
            }
            Float num = jqbzMaterial.getNum();

            for (int i = 0; i < jqylMaterials.size(); i++) {
                JqylMaterial jqylMaterial = jqylMaterials.get(0);
                Float remainingnums = jqylMaterial.getRemainingnums();
                if (remainingnums-num>0){
                    JqbzMaterialList jqbzMaterialList =new JqbzMaterialList();
                    jqbzMaterialList.setPid(jqbzMaterial.getId());
                    jqbzMaterialList.setName(jqylMaterial.getTid().toString());
                    jqbzMaterialList.setNum(num);
                    jqbzMaterialListService.insertJqbzMaterialList(jqbzMaterialList,token);
                    break;
                }else {
                  num =num-remainingnums.floatValue();
                    JqbzMaterialList jqbzMaterialList =new JqbzMaterialList();
                    jqbzMaterialList.setPid(jqbzMaterial.getId());
                    jqbzMaterialList.setName(jqylMaterial.getTid().toString());
                    jqbzMaterialList.setNum(remainingnums.floatValue());
                    jqbzMaterialListService.insertJqbzMaterialList(jqbzMaterialList,token);
                }
            }
        }
        return true;
    }
}
